<template>
  <div class="mod-config">
    <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">
      <el-select v-model="dataForm.selectshopid" placeholder="校区" style="width:150px;height:50px">
        <el-option v-for="(item, index) in shopids" :label="item.shopName" :key="item.shopName" :value="item.id"></el-option>
      </el-select>
      <el-form-item>
        <el-input v-model="dataForm.studentName" placeholder="学生姓名" style="width: 110px;" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-input v-model="dataForm.teacherName" placeholder="打卡老师" style="width: 110px;" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-date-picker
          v-model="dataForm.selDate"
          type="daterange"
          value-format="yyyy-MM-dd"
          align="right"
          unlink-panels
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :picker-options="pickerOptions">
        </el-date-picker>
      </el-form-item>
      <el-form-item v-if="isAuth('business:sign:zjSign')">
        <el-select v-model="dataForm.zjTeacherId" filterable placeholder="打卡助教">
            <el-option
              v-for="item in teacherList"
              :key="item.id"
              :label="item.teacherName"
              :value="item.id">
            </el-option>
          </el-select>
      </el-form-item>
      <el-form-item v-if="isAuth('business:sign:zjSign')">
        <el-date-picker
          v-model="dataForm.selZjDate"
          type="daterange"
          value-format="yyyy-MM-dd"
          align="right"
          unlink-panels
          range-separator="至"
          start-placeholder="助教打卡开始日期"
          end-placeholder="结束日期"
          :picker-options="pickerOptions">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button @click="queryDate()">查询</el-button>
        <!-- <el-button v-if="isAuth('business:sign:save')" type="primary" @click="addOrUpdateHandle()">新增</el-button> -->
        <el-button @click="reload()" type="success">刷新</el-button>
        <el-button v-if="isAuth('business:sign:delete')" type="danger" @click="deleteHandle()" :disabled="dataListSelections.length <= 0">批量删除</el-button>
        <el-button type="primary" @click="exportExcel()" >导出Excel</el-button>
      </el-form-item>
    </el-form>
    <el-table
      :data="dataList"
      border
      v-loading="dataListLoading"
      @selection-change="selectionChangeHandle"
      style="width: 100%;">
      <el-table-column
        type="selection"
        header-align="center"
        align="center"
        width="50">
      </el-table-column>
      <el-table-column
        prop="id"
        header-align="center"
        align="center"
        label="编号" width="100">
      </el-table-column>
      <el-table-column
        prop="courseName"
        header-align="center"
        align="center"
        label="课程">
      </el-table-column>
      <el-table-column
        prop="studentName"
        header-align="center"
        align="center"
        label="学生">
      </el-table-column>
      <el-table-column
        prop="signTime"
        header-align="center"
        align="center"
        label="打卡时间" width="160">
      </el-table-column>
      <!--<el-table-column-->
        <!--prop="teacherid"-->
        <!--header-align="center"-->
        <!--align="center"-->
        <!--label="打卡老师">-->
      <!--</el-table-column>-->
      <el-table-column
        prop="teacherName"
        header-align="center"
        align="center"
        label="打卡老师">
      </el-table-column>
      <el-table-column
        prop="surplus"
        header-align="center"
        align="center"
        label="剩余节数">
      </el-table-column>
      <el-table-column v-if="isAuth('business:sign:zjSign')"
        prop="zjTeacherName"
        header-align="center"
        align="center"
        label="打卡助教">
      </el-table-column>
      <el-table-column v-if="isAuth('business:sign:zjSign')"
        prop="zjSignTime"
        header-align="center"
        align="center"
        label="助教打卡时间">
      </el-table-column>
      <el-table-column
        fixed="right"
        header-align="center"
        align="center"
        width="150"
        label="操作">
        <template slot-scope="scope">
          <!--<el-button type="text" size="small" @click="addOrUpdateHandle(scope.row.id)">修改</el-button>-->
          <el-button v-if="isAuth('business:sign:zjSign')" type="text" size="small" @click="zjSign(scope.row.id)">助教打卡</el-button>
          <el-button type="text" size="small" @click="deleteHandle(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>
    <zj-sign v-if="zjSignVisible" ref="zjSign" @refreshDataList="getDataList"></zj-sign>
  </div>
</template>

<script>
  import AddOrUpdate from './sign-add-or-update'
  import zjSign from './sign-zj'
  export default {
    data () {
      return {
        dataForm: {
          studentName: '',
          teacherName: '',
          selDate: [],
          selectshopid: '',
          selZjDate: [],
          zjTeacherId: ''
        },
        urltoken: this.$cookie.get('token'),
        shopids: '',
        dataList: [],
        pageIndex: 1,
        pageSize: 10,
        totalPage: 0,
        dataListLoading: false,
        dataListSelections: [],
        addOrUpdateVisible: false,
        zjSignVisible: false,
        teacherList: [],
        pickerOptions: {
          shortcuts: [{
            text: '最近一周',
            onClick: function (picker) {
              const end = new Date()
              const start = new Date()
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 7)
              picker.$emit('pick', [start, end])
            }
          }, {
            text: '最近一个月',
            onClick: function (picker) {
              const end = new Date()
              const start = new Date()
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 30)
              picker.$emit('pick', [start, end])
            }
          }, {
            text: '最近三个月',
            onClick: function (picker) {
              const end = new Date()
              const start = new Date()
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 90)
              picker.$emit('pick', [start, end])
            }
          }]
        }
      }
    },
    components: {
      AddOrUpdate,
      zjSign
    },
    activated () {
      this.getTeacherInfo()
      this.getTeacherList()
    },
    methods: {
      // 获取老师的权限
      getTeacherInfo () {
        var userId = this.$store.state.user.id
        this.$http({
          url: this.$http.adornUrl('/business/student/queryTeacherLogin'),
          method: 'get',
          params: {'userId': userId}
        }).then(({data}) => {
          if (data.teacherInfo == null) {
            this.getDataList()
            this.getShopInfo()
          } else {
            this.dataForm.selectshopid = data.teacherInfo.shopId
            this.getDataList()
            this.getShopInfo()
          }
        })
      },
      queryDate () {
        this.pageIndex = 1
        this.getDataList()
      },
      // 获取数据列表
      getDataList () {
        this.dataListLoading = true
        this.$http({
          url: this.$http.adornUrl('/business/sign/list'),
          method: 'get',
          params: this.$http.adornParams({
            'page': this.pageIndex,
            'limit': this.pageSize,
            'studentName': this.dataForm.studentName,
            'teacherName': this.dataForm.teacherName,
            'selectStartDate': this.dataForm.selDate[0],
            'selectEndDate': this.dataForm.selDate[1],
            'selectZjStartDate': this.dataForm.selZjDate[0],
            'selectZjEndDate': this.dataForm.selZjDate[1],
            'selectshopid': this.dataForm.selectshopid,
            'zjTeacherId': this.dataForm.zjTeacherId
          })
        }).then(({data}) => {
          if (data && data.code === 0) {
            // for (var i = 0; i < data.page.list.length; i++) {
            //   this.dataForm.studentid = data.page.list[i].studentid
            //   this.getStudentInfo(i)
            // }
            this.dataList = data.page.list
            this.totalPage = data.page.totalCount
          } else {
            this.dataList = []
            this.totalPage = 0
          }
          this.dataListLoading = false
        })
      },
      // 获取分校信息-列表
      getShopInfo () {
        this.$http({
          url: this.$http.adornUrl('/business/student/getShopInfo'),
          mothod: 'get',
          params: {
            'shopId': ''
          }
        }).then(({
          data
        }) => {
          this.shopids = data.list
        })
      },
      // 每页数
      sizeChangeHandle (val) {
        this.pageSize = val
        this.getDataList()
      },
      // 当前页
      currentChangeHandle (val) {
        this.pageIndex = val
        this.getDataList()
      },
      // 多选
      selectionChangeHandle (val) {
        this.dataListSelections = val
      },
      // 新增 / 修改
      addOrUpdateHandle (id) {
        this.addOrUpdateVisible = true
        this.$nextTick(() => {
          this.$refs.addOrUpdate.init(id)
        })
      },
      getStudentInfo (i) {
        let studentid = this.dataForm.studentid
        this.$http({
          url: this.$http.adornUrl('/business/sign/getStudentInfo'),
          method: 'get',
          params: {'studentid': studentid}
        }).then(({data}) => {
          this.dataList[i].studentid = data.map.studentName
          this.dataList[i].courseid = data.map.name
        })
      },
      // 删除
      deleteHandle (id) {
        var ids = id ? [id] : this.dataListSelections.map(item => {
          return item.id
        })
        this.$confirm(`学生课时将会回退，确定删除?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$http({
            url: this.$http.adornUrl('/business/sign/delete'),
            method: 'post',
            data: this.$http.adornData(ids, false)
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.getDataList()
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        })
      },
      // 导出
      exportExcel () {
        let params = ''
        if (this.dataForm.studentName) {
          params += 'studentName=' + this.dataForm.studentName
        }
        if (this.dataForm.teacherName) {
          params += 'teacherName=' + this.dataForm.teacherName
        }
        if (this.dataForm.selDate[0]) {
          params += 'selectStartDate=' + this.dataForm.selDate[0]
        }
        if (this.dataForm.selDate[1]) {
          params += 'selectEndDate=' + this.dataForm.selDate[1]
        }
        if (this.dataForm.selectshopid) {
          params += 'selectshopid=' + this.dataForm.selectshopid
        }
        if (params) {
          params = '?' + params + '&token=' + this.urltoken
        } else {
          params = '?token=' + this.urltoken
        }
        // location.href = '/babystar-java/business/sign/export' + params
        this.$http({
          url: this.$http.adornUrl('/business/sign/export'),
          method: 'post',
          params: this.$http.adornParams({
            'page': this.pageIndex,
            'limit': this.pageSize,
            'studentName': this.dataForm.studentName,
            'teacherName': this.dataForm.teacherName,
            'selectStartDate': this.dataForm.selDate[0],
            'selectEndDate': this.dataForm.selDate[1],
            'selectshopid': this.dataForm.selectshopid
          }),
          dataType: 'JSONP',
          responseType: 'blob' // 表明返回服务器返回的数据类型 很重要！！
        }).then(({data}) => {
          const link = document.createElement('a')
          link.href = window.URL.createObjectURL(data)
          link.download = '打卡详情.xls' // 下载后文件名
          link.click()
          window.URL.revokeObjectURL(link.href)
        })
      },
      // 助教打卡
      /* zjSign (id) {
        this.$confirm('确定打卡？', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$http({
            url: this.$http.adornUrl('/business/sign/zjSign'),
            method: 'post',
            data: this.$http.adornData(id, false)
          }).then(({data}) => {
            if (data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.getDataList()
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        })
      } */
      zjSign (id) {
        this.zjSignVisible = true
        this.$nextTick(() => {
          this.$refs.zjSign.init(id)
        })
      },
      // 获取助教老师
      getTeacherList () {
        this.$http({
          url: this.$http.adornUrl('/business/teacher/getTeacherList'),
          mothod: 'get',
          params: {

          }
        }).then(({data}) => {
          if (data.code === 0) {
            this.teacherList = data.list
          }
        })
      },
      reload () {
        this.dataForm.studentName = ''
        this.dataForm.teacherName = ''
        this.dataForm.zjTeacherId = ''
        this.dataForm.selDate = []
        this.dataForm.selZjDate = []
        this.getDataList()
      }
    }
  }
</script>
